// index.js
Page({
  data: {
    showRulesModal: false,
    showSettingsModal: false,
    soundEnabled: true,
    vibrationEnabled: true
  },

  onLoad() {
    // 从本地存储读取设置
    try {
      const settings = wx.getStorageSync('gameSettings')
      if (settings) {
        this.setData({
          soundEnabled: settings.soundEnabled,
          vibrationEnabled: settings.vibrationEnabled
        })
      }
    } catch (e) {
      console.error('读取设置失败', e)
    }
  },

  // 导航到设置页面
  goToSettings() {
    wx.navigateTo({
      url: '/pages/settings/settings'
    })
  },

  // 显示规则弹窗
  showRules() {
    this.setData({
      showRulesModal: true
    })
  },

  // 关闭规则弹窗
  closeRules() {
    this.setData({
      showRulesModal: false
    })
  },

  // 显示设置弹窗
  showSettings() {
    this.setData({
      showSettingsModal: true
    })
  },

  // 关闭设置弹窗
  closeSettings() {
    this.setData({
      showSettingsModal: false
    })
    // 保存设置到本地存储
    try {
      wx.setStorageSync('gameSettings', {
        soundEnabled: this.data.soundEnabled,
        vibrationEnabled: this.data.vibrationEnabled
      })
    } catch (e) {
      console.error('保存设置失败', e)
    }
  },

  // 切换音效开关
  toggleSound(e) {
    this.setData({
      soundEnabled: e.detail.value
    })
  },

  // 切换振动开关
  toggleVibration(e) {
    this.setData({
      vibrationEnabled: e.detail.value
    })
  }
})
